package com.aaa.ssm.controller;

import com.aaa.ssm.entity.SysUser;
import com.aaa.ssm.service.SysUserService;
import com.aaa.ssm.service.impl.SysUserServiceImpl;
import com.aaa.ssm.util.Page;
import com.aaa.ssm.util.Result;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @ fileName:SysUserController
 * @ description:
 * @ author:张圣福
 * @ createTime:2023/10/8 20:38
 * @ version:1.0.0
 */
@Controller
/*@RestController*/
@RequestMapping("sysuser")
public class SysUserController extends BaseController{
    @Resource
    private SysUserServiceImpl sysUserService;
    @RequestMapping("toLogin")
    public String tologin(){

        return "redirect:/html/login.html";
    }

    @GetMapping("login")
    @ResponseBody
    public Result login(String userName, String password, HttpSession session){
        SysUser sysUser = sysUserService.checkUser(userName, password);
        if (sysUser!=null){
            session.setAttribute("userInfo",sysUser);
        }
        return success(sysUser);
    }

    @RequestMapping("toAdd")
    public String toAdd(){

        return "redirect:/html/register.html";
    }


    @PutMapping("add")
     @ResponseBody
    public Result add(@RequestBody SysUser sysUser){

        return success(sysUserService.add(sysUser));
    }

    @PostMapping("queryByPage")
     @ResponseBody
    public Result queryByPage(@RequestBody Page<SysUser> page){
        return success(sysUserService.queryByPage(page));
    }
    @GetMapping("queryByParam")
    public Result queryAll(@RequestBody SysUser user){
        List<SysUser> userList = sysUserService.queryAll(user);
        return success(userList);
    }

    @GetMapping("queryById")
     @ResponseBody
    public Result queryById(Integer userId){

        return success(sysUserService.queryById(userId)) ;
    }

    @PutMapping("update")
     @ResponseBody
    public Result update(@RequestBody SysUser sysUser){
        return success(sysUserService.update(sysUser));
    }
    @DeleteMapping("deleteById")
    @ResponseBody
    public Result deleteById(Integer userId){

        return success(sysUserService.deleteById(userId)) ;
    }


    /*
     * 确认分配角色
     * */
    @PostMapping("confirmRole")
    @ResponseBody
    public Result confirmRole(Integer userId, @RequestBody Integer[] checkedRole) {
        return success(sysUserService.confirmAllocationRole(userId,checkedRole));
    }

    /*
     * 删除用户对应的角色
     * */
    @DeleteMapping("deleteUserRole")
    @ResponseBody
    public Result deleteUserRole(Integer userId){
        return success(sysUserService.deleteByUserId(userId));
    }

    /**
     * 退出登录
     * @param session
     * @return
     */
    @GetMapping("logOut")
    @ResponseBody
    public Result logOut( HttpSession session){
        //移除session用户信息
        session.removeAttribute("userInfo");
        //使session失效
        session.invalidate();
        return success("退出成功");
    }
}
